package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
 
 * @author Lucie
 *class Delete_Copy_controler
 *Class variables:msg,flag
 *Class methods:get_all_copys,delete_Copy,get_item,check_input,get_msg,set_msg
 */
public class Delete_Copy_controler {
	

	private String msg; 
	private boolean flag;
	/**
	 * Constructor initializes the class variables
	 */
	public Delete_Copy_controler()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	
	/**
	 * 
	 * @return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	 * @param new_msg
	 * This method chain messages into a string ,
	 *  the purpose ultimately is to display on the screen all the failures
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}
	
/**
 * 
 * @param book_id
 *  *this  method call the class ;serverproxy, to Contact with the server,
	 * we call the method get_AllCopysofItem that return from the db all the copys that relate to the item that exist in the db
	 *  if the item don't have copies we return null
	 * @return ArrayList<String> with all the copies of items id or null if there are not copies for the item
 */
	public   ArrayList<String> get_all_copys(String book_id)
	{
		 ArrayList<String> str_copys=new  ArrayList<String>(); 
		 check_input(book_id);
			if (flag==false)
				 return null;
		str_copys=BusinessImplements.GetInstance().getServerProxy().get_AllCopysofItem(book_id);
		if (str_copys.get(0).equals("false"))
		{	 set_msg("No copies of this book left ");
			 return null;
		}
		
		return str_copys;
		
		
	}
	/**
	 * 
	 * @param copy_id
	 *  this  method call the class ;serverproxy, to Contact with the server,
	 * first we check the Parameters we get (check_input) if something is wrong we return false 
	 *than we continue check by calling the method check_deleteCopy that return "notonshelf" if the copy is not on shelf
	 *or "false" if the item not exist or "ok" if we can continue and delete the copy with the method deleteCopy,
	 * @return  boolean:If the calls in the method failed it returns false otherwise returns true
	 */
	 public boolean delete_Copy(String copy_id)
		{
		 ArrayList<String> str_check=new  ArrayList<String>();
		 	ArrayList<String> str_copy=new  ArrayList<String>();
	 
		 check_input(copy_id);
		 	if (flag==false)
		 return false;
		 	str_check=BusinessImplements.GetInstance().getServerProxy().check_deleteCopy(copy_id);
		 	
		 	if(str_check.get(0).equals("notonshelf"))
		 	{
		 		set_msg("\ncan't delete this iten because is not on shelf"); 
		 		return false;
		 	}
		 	if(str_check.get(0).equals("false"))
		 	{
		 		set_msg("\nitem not exist"); 
		 		return false;
		 	}
		 	if(str_check.get(0).equals("ok"))
		 	{
		 		str_copy=BusinessImplements.GetInstance().getServerProxy().deleteCopy(copy_id);
			
		 	if (str_copy.get(0).equals("false"))
		 	{
		 		set_msg("\nDelete failed\nPlease try again"); 
		 		return false;
		 	}
		 	}
		 	
		 	return flag;
		 	
		 	
}
	 /**
	  * @param id
	  * this  method call the class ;serverproxy, to Contact with the server,
	  * * first we check the Parameters we get (check_input) if something is wrong we return false 
	 * we call the method getItemByID(id) that return from the db all the details of the item 
	 *  if the item don't have copies we return null
	 * @return ArrayList<String> with all the copies of items id or null if there are not copies for the item
	  * @return
	  */
		public ArrayList<String> get_item(String id)
		{
			ArrayList<String> str_item=new  ArrayList<String>(); 
			 check_input(id);
			 if (flag==false)
				 return null;
			 str_item=BusinessImplements.GetInstance().getServerProxy().getItemByID(id);
			 if (str_item.get(0)==null)
			 {
				 set_msg("\nitem number not exist "); 
				 return null;
			 }
			return str_item;
		}
	/**
	 * 
	 * @param book_id
	 * This method checks the parameters we have received are not empty
	 * if they are poses flag= false;
	 */
		public void check_input(String book_id)
		{
			
			if(book_id.equals(""))
			{
				set_msg("\nmising id ");
				flag=false;
			}
		
			
		}

}



